package map.gui.main;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Event;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.io.File;
import java.util.ArrayList;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTabbedPane;
import javax.swing.JTable;
import javax.swing.JToolBar;
import javax.swing.JTree;
import javax.swing.KeyStroke;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;
import javax.swing.border.BevelBorder;
import javax.swing.filechooser.FileFilter;
import javax.swing.table.DefaultTableModel;
import javax.swing.tree.DefaultMutableTreeNode;

import map.adapter.UIAdaptator;
import map.gui.abstraction.AbstractUI;
import map.gui.config.APIModelConfig;
import map.gui.config.DeviceModelConfig;
import map.gui.config.MappingTables;
import map.gui.util.Utils;
import map.gui.util.XMLTreeViewer;
import map.tools.uimodel.UIModelSeriralizer;

public class MainWindow {

	private JFrame jFrame = null;
	private JPanel jContentPane = null;
	private JMenuBar jJMenuBar = null;
	private JMenu fileMenu = null;
	private JMenu editMenu = null;
	private JMenu helpMenu = null;
	private JMenuItem exitMenuItem = null;
	private JMenuItem aboutMenuItem = null;
	private JMenuItem cutMenuItem = null;
	private JMenuItem copyMenuItem = null;
	private JMenuItem pasteMenuItem = null;
	private JMenuItem saveMenuItem = null;
	private JDialog aboutDialog = null;
	private JPanel aboutContentPane = null;
	private JLabel aboutVersionLabel = null;
	private JMenu configMenu = null;
	private JMenuItem apiModelMenuItem = null;
	private JMenuItem devicesMenuItem = null;
	private JMenuItem mapTablesMenuItem = null;
	private JSplitPane jSplitPane = null;
	private JTabbedPane jTabbedPane = null;
	private JTabbedPane jTabbedPane1 = null;
	private JPanel jPanelPropView = null;
	private  JPanel jPanelProject = null;
	private JSplitPane jSplitPane1 = null;
	private JPanel jPanel = null;
	private JPanel jPanel1 = null;
	private JPanel jPanel2Entete = null;
	private JPanel jPanelProp = null;
	private JLabel jLabelProperties = null;
	private JScrollPane jScrollPane = null;
	private JTable jTablePropValue = null;
	private JSplitPane jSplitPane2 = null;
	private JPanel jPanel2Containt = null;
	private JPanel jPanelLink = null;
	private JSplitPane jSplitPaneDUI = null;
	private JPanel jPanelDevices = null;
	private JPanel jPanelUIModel = null;
	private JPanel jPanelDeviceEntete = null;
	private JPanel jPanelDeviceListe = null;
	private JLabel jLabelDevices = null;
	private JPanel jPanelUIModelEntete = null;
	private JPanel jPanelUIModelView = null;
	private JLabel jLabelUIModel = null;
	private JPanel jPanelEnteteLien = null;
	private JPanel jPanelLienContent = null;
	private JLabel jLabelLink = null;
	private JPanel jPanelDeviceProp = null;
	private JPanel jPanelDevicePropList = null;
	private JLabel jLabel = null;
	private JToolBar jJToolBarBar = null;
	private JMenuItem loadFileMenuItem = null;
	private JMenuItem loadProjectMenuItem = null;
	private JSplitPane adaptSplitPane = null;
	private JPanel sourceUIModelPanel = null;
	private JTabbedPane targeUIModelTabbedPane = null;
	private JPanel transformationPanel = null;
	private JPanel uiModelPanel1 = null;
	private JLabel UIModelLabel = null;
	private JScrollPane jScrollPaneUIModel = null;
	private JTree UIModelTree1 = null;
	public JTree projectTree = null;
	private  JFileChooser fc=null ;
	private String uiModel=null;  //  @jve:decl-index=0:
	public static final String XMLFILESPATH=".//models//xml//"; 
	public static final String UIMODELFILESPATH=".//models//ui//";  //  @jve:decl-index=0:
	public static final String APIFILESPATH=".//models//api//";  //  @jve:decl-index=0:
	public static final String DEVICEFILESPATH=".//models//device//";  //  @jve:decl-index=0:
	public static final String TABLEFILESPATH="models//tables//";  //  @jve:decl-index=0:
	public static final String CORRESPO="models//equi//";
	public static MainWindow me=null;  //  @jve:decl-index=0:
	private JButton transformButton = null;
	protected String apiTfile="APIModelXAML_TS.xmi";  //  @jve:decl-index=0:
	public String apiFile="APIModelXAML_PC.xmi";  //  @jve:decl-index=0:
	protected String uiModelTarget;
	private List<JScrollPane> jScrollPane1 = null;  //  @jve:decl-index=0:
	private JScrollPane jScrollPanei = null;
	private List<JPanel> uiModelPaneli = null;
	private List<JTree> uiModelTreei=null;  //  @jve:decl-index=0:
	private XMLTreeViewer treev= null;  //  @jve:decl-index=0:
	private List<XMLTreeViewer> treev1= null;  //  @jve:decl-index=0:
	private JButton transformStep;


	/**
	 * This method initializes jFrame
	 * 
	 * @return javax.swing.JFrame
	 */
	private JFrame getJFrame() {
		if (jFrame == null) {
			jFrame = new JFrame();
			jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
			jFrame.setLocation(new Point(300, 300));
			jFrame.setResizable(true);
			jFrame.setJMenuBar(getJJMenuBar());
			jFrame.setSize(900, 600);
			jFrame.setContentPane(getJContentPane());
			jFrame.setTitle("MAP ");
			me=this;
		}
		return jFrame;
	}

	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			jContentPane = new JPanel();
			jContentPane.setLayout(new BorderLayout());
			jContentPane.add(getJSplitPane(), BorderLayout.CENTER);
		}
		return jContentPane;
	}

	/**
	 * This method initializes jJMenuBar	
	 * 	
	 * @return javax.swing.JMenuBar	
	 */
	private JMenuBar getJJMenuBar() {
		if (jJMenuBar == null) {
			jJMenuBar = new JMenuBar();
			jJMenuBar.add(getFileMenu());
			jJMenuBar.add(getEditMenu());
			jJMenuBar.add(getConfigMenu());
			jJMenuBar.add(getHelpMenu());
		}
		return jJMenuBar;
	}

	/**
	 * This method initializes jMenu	
	 * 	
	 * @return javax.swing.JMenu	
	 */
	private JMenu getFileMenu() {
		if (fileMenu == null) {
			fileMenu = new JMenu();
			fileMenu.setText("File");
			fileMenu.add(getLoadFileMenuItem());
			fileMenu.add(getLoadProjectMenuItem());
			//fileMenu.add(getSaveMenuItem());
			fileMenu.add(getJJToolBarBar());
			fileMenu.add(getExitMenuItem());
		}
		return fileMenu;
	}

	/**
	 * This method initializes jMenu	
	 * 	
	 * @return javax.swing.JMenu	
	 */
	private JMenu getEditMenu() {
		if (editMenu == null) {
			editMenu = new JMenu();
			editMenu.setText("Edit");
			editMenu.add(getCutMenuItem());
			editMenu.add(getCopyMenuItem());
			editMenu.add(getPasteMenuItem());
		}
		return editMenu;
	}

	/**
	 * This method initializes jMenu	
	 * 	
	 * @return javax.swing.JMenu	
	 */
	private JMenu getHelpMenu() {
		if (helpMenu == null) {
			helpMenu = new JMenu();
			helpMenu.setText("Help");
			helpMenu.add(getAboutMenuItem());
		}
		return helpMenu;
	}

	/**
	 * This method initializes jMenuItem	
	 * 	
	 * @return javax.swing.JMenuItem	
	 */
	private JMenuItem getExitMenuItem() {
		if (exitMenuItem == null) {
			exitMenuItem = new JMenuItem();
			exitMenuItem.setText("Exit");
			exitMenuItem.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					System.exit(0);
				}
			});
		}
		return exitMenuItem;
	}

	/**
	 * This method initializes jMenuItem	
	 * 	
	 * @return javax.swing.JMenuItem	
	 */
	private JMenuItem getAboutMenuItem() {
		if (aboutMenuItem == null) {
			aboutMenuItem = new JMenuItem();
			aboutMenuItem.setText("About");
			aboutMenuItem.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					JDialog aboutDialog = getAboutDialog();
					aboutDialog.pack();
					Point loc = getJFrame().getLocation();
					loc.translate(20, 20);
					aboutDialog.setLocation(loc);
					aboutDialog.setVisible(true);
				}
			});
		}
		return aboutMenuItem;
	}

	/**
	 * This method initializes aboutDialog	
	 * 	
	 * @return javax.swing.JDialog
	 */
	private JDialog getAboutDialog() {
		if (aboutDialog == null) {
			aboutDialog = new JDialog(getJFrame(), true);
			aboutDialog.setTitle("About");
			aboutDialog.setLocation(new Point(350, 350));
			aboutDialog.setContentPane(getAboutContentPane());
		}
		return aboutDialog;
	}

	/**
	 * This method initializes aboutContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getAboutContentPane() {
		if (aboutContentPane == null) {
			aboutContentPane = new JPanel();
			aboutContentPane.setLayout(new BorderLayout());
			aboutContentPane.add(getAboutVersionLabel(), BorderLayout.CENTER);
		}
		return aboutContentPane;
	}

	/**
	 * This method initializes aboutVersionLabel	
	 * 	
	 * @return javax.swing.JLabel	
	 */
	private JLabel getAboutVersionLabel() {
		if (aboutVersionLabel == null) {
			aboutVersionLabel = new JLabel();
			aboutVersionLabel.setText("MAP Version 1.0");
			aboutVersionLabel.setHorizontalAlignment(SwingConstants.CENTER);
		}
		return aboutVersionLabel;
	}

	/**
	 * This method initializes jMenuItem	
	 * 	
	 * @return javax.swing.JMenuItem	
	 */
	private JMenuItem getCutMenuItem() {
		if (cutMenuItem == null) {
			cutMenuItem = new JMenuItem();
			cutMenuItem.setText("Cut");
			cutMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X,
					Event.CTRL_MASK, true));
		}
		return cutMenuItem;
	}

	/**
	 * This method initializes jMenuItem	
	 * 	
	 * @return javax.swing.JMenuItem	
	 */
	private JMenuItem getCopyMenuItem() {
		if (copyMenuItem == null) {
			copyMenuItem = new JMenuItem();
			copyMenuItem.setText("Copy");
			copyMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C,
					Event.CTRL_MASK, true));
		}
		return copyMenuItem;
	}

	/**
	 * This method initializes jMenuItem	
	 * 	
	 * @return javax.swing.JMenuItem	
	 */
	private JMenuItem getPasteMenuItem() {
		if (pasteMenuItem == null) {
			pasteMenuItem = new JMenuItem();
			pasteMenuItem.setText("Paste");
			pasteMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V,
					Event.CTRL_MASK, true));
		}
		return pasteMenuItem;
	}

	/**
	 * This method initializes jMenuItem	
	 * 	
	 * @return javax.swing.JMenuItem	
	 */
/*	private JMenuItem getSaveMenuItem() {
		if (saveMenuItem == null) {
			saveMenuItem = new JMenuItem();
			saveMenuItem.setText("Save Project");
			saveMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,
					Event.CTRL_MASK, true));
		}
		return saveMenuItem;
	}
*/
	/**
	 * This method initializes configMenu	
	 * 	
	 * @return javax.swing.JMenu	
	 */
	private JMenu getConfigMenu() {
		if (configMenu == null) {
			configMenu = new JMenu();
			configMenu.setText("Configurations");
			configMenu.add(getApiModelMenuItem());
			configMenu.add(getDevicesMenuItem());
			configMenu.add(getMapTablesMenuItem());
		}
		return configMenu;
	}

	/**
	 * This method initializes apiModelMenuItem	
	 * 	
	 * @return javax.swing.JMenuItem	
	 */
	private JMenuItem getApiModelMenuItem() {
		if (apiModelMenuItem == null) {
			apiModelMenuItem = new JMenuItem();
			apiModelMenuItem.setText("API Model ...");
			apiModelMenuItem.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					JFrame apimodel= new APIModelConfig();
					apimodel.setVisible(true);
				}
			});
		}
		return apiModelMenuItem;
	}

	/**
	 * This method initializes devicesMenuItem	
	 * 	
	 * @return javax.swing.JMenuItem	
	 */
	private JMenuItem getDevicesMenuItem() {
		if (devicesMenuItem == null) {
			devicesMenuItem = new JMenuItem();
			devicesMenuItem.setText("Devices ...");
			devicesMenuItem.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					JFrame devices=new DeviceModelConfig();
					devices.setVisible(true);
				}
			});
		}
		return devicesMenuItem;
	}

	/**
	 * This method initializes mapTablesMenuItem	
	 * 	
	 * @return javax.swing.JMenuItem	
	 */
	private JMenuItem getMapTablesMenuItem() {
		if (mapTablesMenuItem == null) {
			mapTablesMenuItem = new JMenuItem();
			mapTablesMenuItem.setText("Mapping Tables ...");
			mapTablesMenuItem.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					JFrame table = new MappingTables();
					table.setVisible(true);
				}
			});
		}
		return mapTablesMenuItem;
	}

	/**
	 * This method initializes jSplitPane	
	 * 	
	 * @return javax.swing.JSplitPane	
	 */
	private JSplitPane getJSplitPane() {
		if (jSplitPane == null) {
			jSplitPane = new JSplitPane();
			jSplitPane.setDividerLocation(200);
			jSplitPane.setLeftComponent(getJTabbedPane());
			jSplitPane.setRightComponent(getJTabbedPane1());
		}
		return jSplitPane;
	}

	/**
	 * This method initializes jTabbedPane	
	 * 	
	 * @return javax.swing.JTabbedPane	
	 */
	private JTabbedPane getJTabbedPane() {
		if (jTabbedPane == null) {
			jTabbedPane = new JTabbedPane();
			jTabbedPane.setName("Explorer");
			jTabbedPane.addTab("Project", null, getJPanelProject(), null);
		}
		return jTabbedPane;
	}

	/**
	 * This method initializes jTabbedPane1	
	 * 	
	 * @return javax.swing.JTabbedPane	
	 */
	private JTabbedPane getJTabbedPane1() {
		if (jTabbedPane1 == null) {
			jTabbedPane1 = new JTabbedPane();
			jTabbedPane1.setName("View");
			jTabbedPane1.addTab("Transformation", null, getTransformationPanel(), null);
			jTabbedPane1.addTab("Properties view", null, getJPanelPropView(), null);
		}
		return jTabbedPane1;
	}

	/**
	 * This method initializes jPanelPropView	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanelPropView() {
		if (jPanelPropView == null) {
			GridBagConstraints gridBagConstraints = new GridBagConstraints();
			gridBagConstraints.fill = GridBagConstraints.BOTH;
			gridBagConstraints.gridy = 0;
			gridBagConstraints.weightx = 1.0;
			gridBagConstraints.weighty = 1.0;
			gridBagConstraints.gridx = 0;
			jPanelPropView = new JPanel();
			jPanelPropView.setLayout(new GridBagLayout());
			jPanelPropView.add(getJSplitPane1(), gridBagConstraints);
		}
		return jPanelPropView;
	}

	/**
	 * This method initializes jPanelProject	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	public JPanel getJPanelProject() {
		if (jPanelProject == null) {
			GridBagConstraints gridBagConstraints11 = new GridBagConstraints();
			gridBagConstraints11.fill = GridBagConstraints.BOTH;
			gridBagConstraints11.gridy = 0;
			gridBagConstraints11.weightx = 1.0;
			gridBagConstraints11.weighty = 1.0;
			gridBagConstraints11.gridx = 0;
			UIModelLabel = new JLabel();
			UIModelLabel.setText("UIModel");
			jPanelProject = new JPanel();
			jPanelProject.setLayout(new GridBagLayout());
			//jPanelProject.add(getProjectTree(), gridBagConstraints11);
		}
		return jPanelProject;
	}

	/**
	 * This method initializes jSplitPane1	
	 * 	
	 * @return javax.swing.JSplitPane	
	 */
	private JSplitPane getJSplitPane1() {
		if (jSplitPane1 == null) {
			jSplitPane1 = new JSplitPane();
			jSplitPane1.setLeftComponent(getJPanel());
			jSplitPane1.setRightComponent(getJPanel1());
		}
		return jSplitPane1;
	}

	/**
	 * This method initializes jPanel	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanel() {
		if (jPanel == null) {
			jPanel = new JPanel();
			jPanel.setLayout(new BoxLayout(getJPanel(), BoxLayout.Y_AXIS));
			jPanel.setPreferredSize(new Dimension(150, 50));
			jPanel.add(getJPanel2Entete(), null);
			jPanel.add(getJPanelProp(), null);
		}
		return jPanel;
	}

	/**
	 * This method initializes jPanel1	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanel1() {
		if (jPanel1 == null) {
			GridBagConstraints gridBagConstraints2 = new GridBagConstraints();
			gridBagConstraints2.fill = GridBagConstraints.BOTH;
			gridBagConstraints2.gridy = 0;
			gridBagConstraints2.weightx = 1.0;
			gridBagConstraints2.weighty = 1.0;
			gridBagConstraints2.gridx = 0;
			jPanel1 = new JPanel();
			jPanel1.setLayout(new GridBagLayout());
			jPanel1.add(getJSplitPane2(), gridBagConstraints2);
		}
		return jPanel1;
	}

	/**
	 * This method initializes jPanel2Entete	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanel2Entete() {
		if (jPanel2Entete == null) {
			jLabelProperties = new JLabel();
			jLabelProperties.setText("Properties");
			jPanel2Entete = new JPanel();
			jPanel2Entete.setLayout(new GridBagLayout());
			jPanel2Entete.setPreferredSize(new Dimension(60, 10));
			jPanel2Entete.setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED));
			jPanel2Entete.setBackground(Color.gray);
			jPanel2Entete.add(jLabelProperties, new GridBagConstraints());
		}
		return jPanel2Entete;
	}

	/**
	 * This method initializes jPanelProp	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanelProp() {
		if (jPanelProp == null) {
			GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
			gridBagConstraints1.fill = GridBagConstraints.BOTH;
			gridBagConstraints1.gridy = 0;
			gridBagConstraints1.weightx = 1.0;
			gridBagConstraints1.weighty = 1.0;
			gridBagConstraints1.gridx = 0;
			jPanelProp = new JPanel();
			jPanelProp.setLayout(new GridBagLayout());
			jPanelProp.setPreferredSize(new Dimension(0, 490));
			jPanelProp.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
			jPanelProp.add(getJScrollPane(), gridBagConstraints1);
		}
		return jPanelProp;
	}

	/**
	 * This method initializes jScrollPane	
	 * 	
	 * @return javax.swing.JScrollPane	
	 */
	private JScrollPane getJScrollPane() {
		if (jScrollPane == null) {
			jScrollPane = new JScrollPane();
			jScrollPane.setViewportView(getJTablePropValue());
		}
		return jScrollPane;
	}

	/**
	 * This method initializes jTablePropValue	
	 * 	
	 * @return javax.swing.JTable	
	 */
	private JTable getJTablePropValue() {
		if (jTablePropValue == null) {
			DefaultTableModel defaultTableModel = new DefaultTableModel();
			defaultTableModel.setColumnCount(2);
			defaultTableModel.setRowCount(20);
			jTablePropValue = new JTable();
			jTablePropValue.setModel(defaultTableModel);
		}
		return jTablePropValue;
	}

	/**
	 * This method initializes jSplitPane2	
	 * 	
	 * @return javax.swing.JSplitPane	
	 */
	private JSplitPane getJSplitPane2() {
		if (jSplitPane2 == null) {
			jSplitPane2 = new JSplitPane();
			jSplitPane2.setOrientation(JSplitPane.VERTICAL_SPLIT);
			jSplitPane2.setTopComponent(getJPanel2Containt());
			jSplitPane2.setBottomComponent(getJPanelLink());
			jSplitPane2.setDividerLocation(350);
		}
		return jSplitPane2;
	}

	/**
	 * This method initializes jPanel2Containt	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanel2Containt() {
		if (jPanel2Containt == null) {
			GridBagConstraints gridBagConstraints3 = new GridBagConstraints();
			gridBagConstraints3.fill = GridBagConstraints.BOTH;
			gridBagConstraints3.gridy = 0;
			gridBagConstraints3.weightx = 1.0;
			gridBagConstraints3.weighty = 1.0;
			gridBagConstraints3.gridx = 0;
			jPanel2Containt = new JPanel();
			jPanel2Containt.setLayout(new GridBagLayout());
			jPanel2Containt.add(getJSplitPaneDUI(), gridBagConstraints3);
		}
		return jPanel2Containt;
	}

	/**
	 * This method initializes jPanelLink	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanelLink() {
		if (jPanelLink == null) {
			jPanelLink = new JPanel();
			jPanelLink.setLayout(new BoxLayout(getJPanelLink(), BoxLayout.Y_AXIS));
			jPanelLink.add(getJPanelEnteteLien(), null);
			jPanelLink.add(getJPanelLienContent(), null);
		}
		return jPanelLink;
	}

	/**
	 * This method initializes jSplitPaneDUI	
	 * 	
	 * @return javax.swing.JSplitPane	
	 */
	private JSplitPane getJSplitPaneDUI() {
		if (jSplitPaneDUI == null) {
			jSplitPaneDUI = new JSplitPane();
			jSplitPaneDUI.setDividerLocation(201);
			jSplitPaneDUI.setLeftComponent(getJPanelDevices());
			jSplitPaneDUI.setRightComponent(getJPanelUIModel());
		}
		return jSplitPaneDUI;
	}

	/**
	 * This method initializes jPanelDevices	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanelDevices() {
		if (jPanelDevices == null) {
			jPanelDevices = new JPanel();
			jPanelDevices.setLayout(new BoxLayout(getJPanelDevices(), BoxLayout.Y_AXIS));
			jPanelDevices.setPreferredSize(new Dimension(100, 0));
			jPanelDevices.add(getJPanelDeviceEntete(), null);
			jPanelDevices.add(getJPanelDeviceListe(), null);
			jPanelDevices.add(getJPanelDeviceProp(), null);
			jPanelDevices.add(getJPanelDevicePropList(), null);
		}
		return jPanelDevices;
	}

	/**
	 * This method initializes jPanelUIModel	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanelUIModel() {
		if (jPanelUIModel == null) {
			jPanelUIModel = new JPanel();
			jPanelUIModel.setLayout(new BoxLayout(getJPanelUIModel(), BoxLayout.Y_AXIS));
			jPanelUIModel.add(getJPanelUIModelEntete(), null);
			jPanelUIModel.add(getJPanelUIModelView(), null);
		}
		return jPanelUIModel;
	}

	/**
	 * This method initializes jPanelDeviceEntete	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanelDeviceEntete() {
		if (jPanelDeviceEntete == null) {
			GridBagConstraints gridBagConstraints4 = new GridBagConstraints();
			gridBagConstraints4.gridx = 0;
			gridBagConstraints4.gridy = 0;
			jLabelDevices = new JLabel();
			jLabelDevices.setText("Devices");
			jLabelDevices.setVerticalAlignment(SwingConstants.TOP);
			jLabelDevices.setVerticalTextPosition(SwingConstants.TOP);
			jLabelDevices.setPreferredSize(new Dimension(60, 16));
			jPanelDeviceEntete = new JPanel();
			jPanelDeviceEntete.setLayout(new GridBagLayout());
			jPanelDeviceEntete.setToolTipText("");
			jPanelDeviceEntete.setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED));
			jPanelDeviceEntete.setBackground(Color.gray);
			jPanelDeviceEntete.setPreferredSize(new Dimension(60, 10));
			jPanelDeviceEntete.add(jLabelDevices, gridBagConstraints4);
		}
		return jPanelDeviceEntete;
	}

	/**
	 * This method initializes jPanelDeviceListe	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanelDeviceListe() {
		if (jPanelDeviceListe == null) {
			jPanelDeviceListe = new JPanel();
			jPanelDeviceListe.setLayout(new GridBagLayout());
			jPanelDeviceListe.setPreferredSize(new Dimension(400, 140));
		}
		return jPanelDeviceListe;
	}

	/**
	 * This method initializes jPanelUIModelEntete	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanelUIModelEntete() {
		if (jPanelUIModelEntete == null) {
			GridBagConstraints gridBagConstraints5 = new GridBagConstraints();
			gridBagConstraints5.gridx = 0;
			gridBagConstraints5.gridy = 0;
			jLabelUIModel = new JLabel();
			jLabelUIModel.setText("UI Model");
			jPanelUIModelEntete = new JPanel();
			jPanelUIModelEntete.setLayout(new GridBagLayout());
			jPanelUIModelEntete.setBackground(Color.gray);
			jPanelUIModelEntete.setPreferredSize(new Dimension(60, 10));
			jPanelUIModelEntete.add(jLabelUIModel, gridBagConstraints5);
		}
		return jPanelUIModelEntete;
	}

	/**
	 * This method initializes jPanelUIModelView	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanelUIModelView() {
		if (jPanelUIModelView == null) {
			jPanelUIModelView = new JPanel();
			jPanelUIModelView.setLayout(new GridBagLayout());
			jPanelUIModelView.setPreferredSize(new Dimension(400, 320));
		}
		return jPanelUIModelView;
	}

	/**
	 * This method initializes jPanelEnteteLien	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanelEnteteLien() {
		if (jPanelEnteteLien == null) {
			GridBagConstraints gridBagConstraints6 = new GridBagConstraints();
			gridBagConstraints6.gridx = 0;
			gridBagConstraints6.gridy = 0;
			jLabelLink = new JLabel();
			jLabelLink.setText("Link Table");
			jPanelEnteteLien = new JPanel();
			jPanelEnteteLien.setLayout(new GridBagLayout());
			jPanelEnteteLien.setPreferredSize(new Dimension(58, 10));
			jPanelEnteteLien.setBackground(Color.gray);
			jPanelEnteteLien.add(jLabelLink, gridBagConstraints6);
		}
		return jPanelEnteteLien;
	}

	/**
	 * This method initializes jPanelLienContent	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanelLienContent() {
		if (jPanelLienContent == null) {
			jPanelLienContent = new JPanel();
			jPanelLienContent.setLayout(new GridBagLayout());
			jPanelLienContent.setPreferredSize(new Dimension(100, 110));
		}
		return jPanelLienContent;
	}

	/**
	 * This method initializes jPanelDeviceProp	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanelDeviceProp() {
		if (jPanelDeviceProp == null) {
			GridBagConstraints gridBagConstraints7 = new GridBagConstraints();
			gridBagConstraints7.gridx = 0;
			gridBagConstraints7.gridy = 0;
			jLabel = new JLabel();
			jLabel.setText("Device Properties");
			jPanelDeviceProp = new JPanel();
			jPanelDeviceProp.setLayout(new GridBagLayout());
			jPanelDeviceProp.setPreferredSize(new Dimension(0, 10));
			jPanelDeviceProp.setBackground(Color.gray);
			jPanelDeviceProp.add(jLabel, gridBagConstraints7);
		}
		return jPanelDeviceProp;
	}

	/**
	 * This method initializes jPanelDevicePropList	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanelDevicePropList() {
		if (jPanelDevicePropList == null) {
			jPanelDevicePropList = new JPanel();
			jPanelDevicePropList.setLayout(new GridBagLayout());
			jPanelDevicePropList.setPreferredSize(new Dimension(0, 140));
		}
		return jPanelDevicePropList;
	}

	/**
	 * This method initializes jJToolBarBar	
	 * 	
	 * @return javax.swing.JToolBar	
	 */
	private JToolBar getJJToolBarBar() {
		if (jJToolBarBar == null) {
			jJToolBarBar = new JToolBar();
		}
		return jJToolBarBar;
	}

	/**
	 * This method initializes loadFileMenuItem	
	 * 	
	 * @return javax.swing.JMenuItem	
	 */
	private JMenuItem getLoadFileMenuItem() {
		if (loadFileMenuItem == null) {
			loadFileMenuItem = new JMenuItem();
			loadFileMenuItem.setText("Abstract ...");
			loadFileMenuItem.addActionListener(new java.awt.event.ActionListener() {   
				public void actionPerformed(java.awt.event.ActionEvent e) {    
					JFrame frame= new AbstractUI(me);
					frame.setVisible(true);
				}
			
			});
		}
		return loadFileMenuItem;
	}

	/**
	 * This method initializes loadProjectMenuItem	
	 * 	
	 * @return javax.swing.JMenuItem	
	 */
	private JMenuItem getLoadProjectMenuItem() {
		if(fc==null)
			fc = new JFileChooser();
			fc.addChoosableFileFilter(new FileFilter() {
				public boolean accept(File f) {
				    if (f.isDirectory()) {
					return true;
				    }

				    String extension = Utils.getExtension(f);
				    if (extension != null) {
				    	boolean eq=false;
				    	for(int i=0; i<Utils.extensions.length;i++)
				    		if(extension.equals(Utils.extensions[i]))
				    			eq=true;
					        return eq;
				    }
				    return false;
				}

				 //The description of this filter
			    public String getDescription() {
			        return "Just Texts";
			    }
			});


		if (loadProjectMenuItem == null) {
			loadProjectMenuItem = new JMenuItem();
			loadProjectMenuItem.setText("Load Model...");
			loadProjectMenuItem.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
			        int returnVal = fc.showOpenDialog(MainWindow.this.jFrame);
			        //System.out.println(returnVal+" "+JFileChooser.APPROVE_OPTION);
			        if (returnVal == JFileChooser.APPROVE_OPTION) {
			            setUiModel((fc.getSelectedFile().getName()));
			            apiFile="APIModelXAML_PC.xmi";
					    DefaultMutableTreeNode top = new DefaultMutableTreeNode("UIModel Project");
					    DefaultMutableTreeNode uimodel = new DefaultMutableTreeNode("UIModel files");
					    DefaultMutableTreeNode apimodel = new DefaultMutableTreeNode("API Models files");
					    uimodel.add( new DefaultMutableTreeNode(getUiModel()));
					    apimodel.add(new DefaultMutableTreeNode(apiFile));
					    top.add(uimodel);
					    top.add(apimodel);
					    jPanelProject.removeAll();
						GridBagConstraints gridBagConstraints11 = new GridBagConstraints();
						gridBagConstraints11.fill = GridBagConstraints.BOTH;
						gridBagConstraints11.gridy = 0;
						gridBagConstraints11.weightx = 1.0;
						gridBagConstraints11.weighty = 1.0;
						gridBagConstraints11.gridx = 0;
					    jPanelProject.add(getProjectTree(top), gridBagConstraints11);;
					    MainWindow.this.jFrame.validate();
					    setUiModel(null);
					    targeUIModelTabbedPane.removeAll();
					    
			        } else {
			        }
			   }

			});
		}
		return loadProjectMenuItem;
	}

	/**
	 * This method initializes adaptSplitPane	
	 * 	
	 * @return javax.swing.JSplitPane	
	 */
	private JSplitPane getAdaptSplitPane() {
		if (adaptSplitPane == null) {
			adaptSplitPane = new JSplitPane();
			adaptSplitPane.setLeftComponent(getSourceUIModelPanel());
			adaptSplitPane.setRightComponent(getTargeUIModelTabbedPane());
		}
		return adaptSplitPane;
	}

	/**
	 * This method initializes sourceUIModelPanel	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getSourceUIModelPanel() {
		if (sourceUIModelPanel == null) {
			GridBagConstraints gridBagConstraints12 = new GridBagConstraints();
			gridBagConstraints12.gridx = 0;
			gridBagConstraints12.gridy = 4;
			GridBagConstraints gridBagConstraints10 = new GridBagConstraints();
			gridBagConstraints10.insets = new Insets(0, 0, 5, 0);
			gridBagConstraints10.fill = GridBagConstraints.BOTH;
			gridBagConstraints10.gridy = 2;
			gridBagConstraints10.weightx = 1.0;
			gridBagConstraints10.weighty = 1.0;
			gridBagConstraints10.gridx = 0;
			GridBagConstraints gridBagConstraints9 = new GridBagConstraints();
			gridBagConstraints9.insets = new Insets(0, 0, 5, 0);
			gridBagConstraints9.gridx = 0;
			gridBagConstraints9.gridy = 0;
			sourceUIModelPanel = new JPanel();
			sourceUIModelPanel.setLayout(new GridBagLayout());
			sourceUIModelPanel.setPreferredSize(new Dimension(200, 0));
			sourceUIModelPanel.add(UIModelLabel, gridBagConstraints9);
			sourceUIModelPanel.add(getJScrollPaneUIModel(), gridBagConstraints10);
			GridBagConstraints gbc_transformStep = new GridBagConstraints();
			gbc_transformStep.insets = new Insets(0, 0, 5, 0);
			gbc_transformStep.gridx = 0;
			gbc_transformStep.gridy = 3;
			sourceUIModelPanel.add(getTransformStep(), gbc_transformStep);
			sourceUIModelPanel.add(getTransformButton(), gridBagConstraints12);
		}
		return sourceUIModelPanel;
	}

	/**
	 * This method initializes targeUIModelTabbedPane	
	 * 	
	 * @return javax.swing.JTabbedPane	
	 */
	private JTabbedPane getTargeUIModelTabbedPane() {
		if (targeUIModelTabbedPane == null) {
			targeUIModelTabbedPane = new JTabbedPane();
			targeUIModelTabbedPane.addTab("UI Model 1", null, getUiModelPanel1(), null);
		}
		return targeUIModelTabbedPane;
	}

	/**
	 * This method initializes targeUIModelTabbedPane	
	 * 	
	 * @return javax.swing.JTabbedPane	
	 */
	private JTabbedPane getTargeUIModelTabbedPane(List<JPanel> treePanel ) {
		int i=1;
		if (targeUIModelTabbedPane == null) {
			targeUIModelTabbedPane = new JTabbedPane();
		}
		else
			targeUIModelTabbedPane.removeAll();
		for(JPanel jp:treePanel)
		{
			targeUIModelTabbedPane.addTab("UI Model "+i++, null, jp, null);
		}
		
		return targeUIModelTabbedPane;
	}
	/**
	 * This method initializes transformationPanel	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getTransformationPanel() {
		if (transformationPanel == null) {
			GridBagConstraints gridBagConstraints8 = new GridBagConstraints();
			gridBagConstraints8.fill = GridBagConstraints.BOTH;
			gridBagConstraints8.weighty = 1.0;
			gridBagConstraints8.weightx = 1.0;
			transformationPanel = new JPanel();
			transformationPanel.setLayout(new GridBagLayout());
			transformationPanel.add(getAdaptSplitPane(), gridBagConstraints8);
		}
		return transformationPanel;
	}

	/**
	 * This method initializes uiModelPanel1	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getUiModelPanel1() {
		if (uiModelPanel1 == null) {
			GridBagConstraints gridBagConstraints13 = new GridBagConstraints();
			gridBagConstraints13.fill = GridBagConstraints.BOTH;
			gridBagConstraints13.weighty = 1.0;
			gridBagConstraints13.weightx = 1.0;
			uiModelPanel1 = new JPanel();
			uiModelPanel1.setLayout(new GridBagLayout());
			
			uiModelPanel1.add (getJScrollPane1(), gridBagConstraints13);
		}
		return uiModelPanel1;
	}

	/**
	 * This method initializes uiModelPanel1	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private List<JPanel> getUiModelPanel1(List<JScrollPane> ljsp) {
		if (uiModelPaneli == null) {
			uiModelPaneli = new ArrayList<JPanel>();
		}
		else
			uiModelPaneli.clear();
		for(JScrollPane js:ljsp){
			GridBagConstraints gridBagConstraints13 = new GridBagConstraints();
			gridBagConstraints13.fill = GridBagConstraints.BOTH;
			gridBagConstraints13.weighty = 1.0;
			gridBagConstraints13.weightx = 1.0;
			JPanel jp = new JPanel();
			jp.setLayout(new GridBagLayout());

			jp.add (js, gridBagConstraints13);
			uiModelPaneli.add(jp);
		}
		return uiModelPaneli;
	}

	/**
	 * This method initializes UIModelTree1	
	 * 	
	 * @return javax.swing.JTree	
	 */
	private JTree getUIModelTree1() {
		if (UIModelTree1 == null) {
			UIModelTree1 = new JTree();
			if(treev==null)
				treev= new XMLTreeViewer(UIModelTree1);
//			UIModelTree1.setPreferredSize(new Dimension(600, 22));
//			UIModelTree1.setShowsRootHandles(true);
			UIModelTree1.setVisible(false);
		}
		return UIModelTree1;
	}


	/**
	 * This method initializes jScrollPaneUIModel	
	 * 	
	 * @return javax.swing.JScrollPane	
	 */
	private JScrollPane getJScrollPaneUIModel() {
		if (jScrollPaneUIModel == null) {
			jScrollPaneUIModel = new JScrollPane( );
			jScrollPaneUIModel.setPreferredSize(new Dimension(600, 22));
			jScrollPaneUIModel.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
			jScrollPaneUIModel.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
			jScrollPaneUIModel.setViewportBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED));
			jScrollPaneUIModel.setViewportView(getUIModelTree1());

		}
		return jScrollPaneUIModel;
	}

	private List<JScrollPane> getJScrollPaneUIModel(List<JTree> ltree ) {
		if (jScrollPane1 == null) {
			jScrollPane1 = new  ArrayList<JScrollPane>();
		}
		else
			jScrollPane1.clear(); 
		for(JTree jt:ltree){
			JScrollPane js=new JScrollPane( );
			 js.setPreferredSize(new Dimension(600, 22));
			 js.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
			 js.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
			 js.setViewportBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED));
			 js.setViewportView(jt);
			 jScrollPane1.add(js);
		}
		return jScrollPane1;
	}

 	
	public List<JTree> getTreeUIModel(int n){
		if(uiModelTreei==null)
		{
			uiModelTreei= new ArrayList<JTree>();
			if(treev1==null)
				treev1= new ArrayList<XMLTreeViewer>();
		}
		else 
		{	
			uiModelTreei.clear();
			treev1.clear();
		
		}
		for(int i=0;i<n;i++){
			JTree tj=new JTree();
			uiModelTreei.add(tj);
			treev1.add(new XMLTreeViewer(tj));
		}
		return uiModelTreei;
	}

	public JTree getProjectTree(DefaultMutableTreeNode top) {
		projectTree = new JTree(top);
		projectTree
				.addTreeSelectionListener(new javax.swing.event.TreeSelectionListener() {
					public void valueChanged(javax.swing.event.TreeSelectionEvent e) {
						//System.out.println("valueChanged()");  
					    DefaultMutableTreeNode node = (DefaultMutableTreeNode)
	                       projectTree.getLastSelectedPathComponent();
						    if (node == null)
						    //Nothing is selected.	
						    return;
						    if (node.isLeaf()) {
						    	if(node.getParent().toString().contains("UIModel"))
						    	{ 
									if(getUiModel()==null)
									{
										setUiModel(node.toString());
										try {
											treev.xmlSetUp(new File(UIMODELFILESPATH+getUiModel()));
											UIModelTree1.setVisible(true);
											transformButton.setEnabled(true);
											transformStep.setEnabled(true);
											
											
										} catch (Exception e1) {
												e1.printStackTrace();
										}
									}

						    	}
						    	else
						    	{ 
						    		setUiModel(null);
									treev.xmlSetUp(new File(UIMODELFILESPATH+node.toString()));
									UIModelTree1.setVisible(true);
						    	}
						        
						    } else {
						    	//System.out.println("valueChanged()  not node.isLeaf()"); 
						    }
				
					}
			});	
		MainWindow.this.jFrame.validate();
		return projectTree;
	}

	/**
	 * This method initializes transformButton	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getTransformButton() {
		if (transformButton == null) {
			transformButton = new JButton();
			transformButton.setText("Transform");
			transformButton.setEnabled(false);
			transformButton.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					UIAdaptator adap= new UIAdaptator(APIFILESPATH+apiFile,APIFILESPATH+apiTfile);
					adap.sourceUIModel=(new UIModelSeriralizer().load(new File(UIMODELFILESPATH+getUiModel())));
					uiModelTarget=UIMODELFILESPATH+getUiModel().substring(0, getUiModel().length()-3)+"Target.xmi";
					adap.processUIModel();
					List<String> file= new ArrayList<String>();
					for(int index=0;index<adap.targetUIModelMap.size();index++){
						String name=UIMODELFILESPATH+getUiModel().substring(0, getUiModel().length()-3)+"Target"+(index+1)+".xmi";
						if(adap.targetUIModelMap.get(index)!=null)
						{	
							new UIModelSeriralizer().save(adap.targetUIModelMap.get(index), name);
							file.add(name);
						}
						if(index>10)
							break;
					}
					targeUIModelTabbedPane= getTargeUIModelTabbedPane(getUiModelPanel1(getJScrollPaneUIModel(getTreeUIModel(file.size()))));
					for(int i=0;i<file.size();i++){
						try {
							treev1.get(i).xmlSetUp(new File( file.get(i)));
						} catch (Exception e1) {
							e1.printStackTrace();
						} 
					}					
					MainWindow.this.jFrame.validate();

				}
			});
		}
		return transformButton;
	}

	/**
	 * This method initializes jScrollPane1	
	 * 	
	 * @return javax.swing.JScrollPane	
	 */
	private JScrollPane getJScrollPane1() {
		if (jScrollPanei == null) {
			jScrollPanei = new JScrollPane();
		}
		return jScrollPanei;
	}

	/**
	 * Launches this application
	 */
	public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				MainWindow application = new MainWindow();
				application.getJFrame().setVisible(true);
			}
		});
	}

	public void setUiModel(String uiModel) {
		this.uiModel = uiModel;
	}

	public String getUiModel() {
		return uiModel;
	}

	public void setSaveMenuItem(JMenuItem saveMenuItem) {
		this.saveMenuItem = saveMenuItem;
	}

	public JMenuItem getSaveMenuItem() {
		return saveMenuItem;
	}

	private JButton getTransformStep() {
		if (transformStep == null) {
			transformStep = new JButton("TransformStep");
			transformStep.setEnabled(false);
			transformStep.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) { //
					UIAdaptator adap= new UIAdaptator(APIFILESPATH+apiFile,APIFILESPATH+apiTfile ,CORRESPO+getUiModel().substring(0, getUiModel().length()-3)+"Target1.xmi");
					adap.sourceUIModel=(new UIModelSeriralizer().load(new File(UIMODELFILESPATH+getUiModel())));
					uiModelTarget=UIMODELFILESPATH+getUiModel().substring(0, getUiModel().length()-3)+"Target.xmi";
					adap.processStep();
					List<String> file= new ArrayList<String>();
					for(int index=0;index<adap.targetUIModelMap.size();index++){
						String name=UIMODELFILESPATH+getUiModel().substring(0, getUiModel().length()-3)+"Target"+(index+1)+".xmi";
						if(adap.targetUIModelMap.get(index)!=null)
						{	
							new UIModelSeriralizer().save(adap.targetUIModelMap.get(index), name);
							file.add(name);
						}
						if(index>10)
							break;
					}
					targeUIModelTabbedPane= getTargeUIModelTabbedPane(getUiModelPanel1(getJScrollPaneUIModel(getTreeUIModel(file.size()))));
					for(int i=0;i<file.size();i++){
						try {
							treev1.get(i).xmlSetUp(new File( file.get(i)));
						} catch (Exception e1) {
							e1.printStackTrace();
						} 
					}					
					MainWindow.this.jFrame.validate();
				}
			});
		}
		return transformStep;
	}
}
